﻿using System;
using MathNet.Numerics.LinearAlgebra.Double;
using MathNet.Numerics.LinearAlgebra.Generic;

namespace Marvin.MathematicalFunctions
{
    class LinearFunction: IMathematicalFunction<double>
    {
        private readonly Vector<double> _theta; 

        public LinearFunction(Vector<double> theta)
        {
            _theta = theta; 
        }

        public double Calculate(double[] x)
        {
            var vector = new DenseVector(x.Length + 1);
            vector[0] = 1;
            CopyArrayToVector(x, vector, 1);
            return Calculate( vector );            
        }

        private static void CopyArrayToVector(double[] x, Vector<double> vector, int destination)
        {
            for (int i = destination; i < vector.Count; i++)
            {
                vector[i] = x[i - destination];
            }
        }

        public double Calculate(DenseVector x)
        {
            return _theta.DotProduct(x); 
        }
    }
}
